package ch.nostromo.tiffanys.tests;

import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

import junit.framework.TestCase;
import ch.nostromo.lib.logging.NosLoggingConsoleFormatter;
import ch.nostromo.tiffanys.engines.Engine;
import ch.nostromo.tiffanys.engines.EngineSettings;
import ch.nostromo.tiffanys.game.player.PlayerEngine;
import ch.nostromo.tiffanys.game.player.PlayerHuman;

public class TestBase extends TestCase {

  private Level logLevel = Level.INFO;

  protected Logger logger = Logger.getLogger(getClass().getName());
  
  protected static final String DEFAULT_ENGINE = "ch.nostromo.tiffanys.engines.tiffanys.TiffanysEngine";


  @SuppressWarnings("unchecked")
  protected Class<Engine> getCurrentDefaultEngine() throws ClassNotFoundException {
    return (Class<Engine>) Class.forName(DEFAULT_ENGINE);
  }

  protected EngineSettings getEngineSettingsWithFixedDepth(int depth) {
    EngineSettings engineSettings = new EngineSettings();
    engineSettings.GAME_MODE = EngineSettings.GAME_MODE_FIXED_DEPTH;
    engineSettings.FIXED_DEPTH = depth;
    return engineSettings;
  }

  protected EngineSettings getEngineSettingsWithFixedSecPerMove(int sec) {
    EngineSettings engineSettings = new EngineSettings();
    engineSettings.GAME_MODE = EngineSettings.GAME_MODE_FIXED_TIME_PER_MOVE;
    engineSettings.FIXED_TIME_PER_MOVE = sec;
    return engineSettings;
  }
  
  protected PlayerEngine getEngineWithFixedDepth(int depth) throws ClassNotFoundException {
    Class<Engine> engineClass = getCurrentDefaultEngine();
    return new PlayerEngine(engineClass, getEngineSettingsWithFixedDepth(depth));
  }
  
  protected PlayerEngine getEngineWithFixedSecPerMove(int sec) throws ClassNotFoundException {
    Class<Engine> engineClass = getCurrentDefaultEngine();
    return new PlayerEngine(engineClass, getEngineSettingsWithFixedSecPerMove(sec));
  }
  
  protected PlayerHuman getHumanPlayer() {
    return new PlayerHuman("Human Player");
  }
  
  @Override
  protected void setUp() throws Exception {
    super.setUp();
    LogManager.getLogManager().reset();
    Logger root = Logger.getLogger("ch.nostromo");
    root.setLevel(logLevel);
    LogManager.getLogManager().addLogger(root);

    ConsoleHandler consoleHandler = new ConsoleHandler();
    consoleHandler.setFormatter(new NosLoggingConsoleFormatter());
    consoleHandler.setLevel(logLevel);
    LogManager.getLogManager().getLogger("").addHandler(consoleHandler);
  }

  @Override
  protected void tearDown() throws Exception {
    super.tearDown();
  }

}
